package com.burst.reptile.modular.monitor.controller;

import java.util.List;

import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.burst.reptile.common.exception.job.TaskException;
import com.burst.reptile.common.utils.poi.ExcelUtil;
import com.burst.reptile.framework.aspectj.lang.annotation.Log;
import com.burst.reptile.framework.aspectj.lang.enums.BusinessType;
import com.burst.reptile.framework.web.controller.BaseController;
import com.burst.reptile.framework.web.result.LayuiResult;
import com.burst.reptile.framework.web.result.Result;
import com.burst.reptile.modular.monitor.entity.SysJob;
import com.burst.reptile.modular.monitor.service.ISysJobService;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 定时任务调度表 前端控制器
 * </p>
 *
 * @author wujin
 * @since 2020-06-09
 */
@RestController
@RequestMapping("/monitor/job")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysJobController extends BaseController{

	private final ISysJobService jobService;

	/**
	 * 查询定时任务列表
	 */
	@PreAuthorize("@ss.hasPermi('monitor:job:list')")
	@GetMapping("/list")
	public LayuiResult<Object> list(SysJob sysJob) {
		Page<SysJob> page = startPage();
		Page<SysJob> list = jobService.selectPageJobList(page, sysJob);
		return LayuiResult.success(list.getTotal(), list.getRecords());
	}

	/**
	 * 导出定时任务列表
	 */
	@PreAuthorize("@ss.hasPermi('monitor:job:export')")
	@Log(title = "定时任务", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public Result<Object> export(SysJob sysJob) {
		List<SysJob> list = jobService.selectJobList(sysJob);
		ExcelUtil<SysJob> util = new ExcelUtil<SysJob>(SysJob.class);
		return util.exportExcel(list, "定时任务");
	}

	/**
	 * 获取定时任务详细信息
	 */
	@PreAuthorize("@ss.hasPermi('monitor:job:query')")
	@GetMapping(value = "/detailed/{jobId}")
	public Result<Object> getInfo(@PathVariable("jobId") Long jobId) {
		return Result.success(jobService.selectJobById(jobId));
	}

	/**
	 * 新增定时任务
	 */
	@PreAuthorize("@ss.hasPermi('monitor:job:add')")
	@Log(title = "定时任务", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	public Result<Object> add(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
		return Result.success(jobService.insertJob(sysJob));
	}

	/**
	 * 修改定时任务
	 */
	@PreAuthorize("@ss.hasPermi('monitor:job:edit')")
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@PutMapping("/edit")
	public Result<Object> edit(@RequestBody SysJob sysJob) throws SchedulerException, TaskException {
		return Result.success(jobService.updateJob(sysJob));
	}

	/**
	 * 定时任务状态修改
	 */
	@PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@PutMapping("/changeStatus")
	public Result<Object> changeStatus(@RequestBody SysJob job) throws SchedulerException {
		SysJob newJob = jobService.selectJobById(job.getJobId());
		newJob.setJobStatus(job.getJobStatus());
		return Result.success(jobService.changeStatus(newJob));
	}

	/**
	 * 定时任务立即执行一次
	 */
	@PreAuthorize("@ss.hasPermi('monitor:job:changeStatus')")
	@Log(title = "定时任务", businessType = BusinessType.UPDATE)
	@PutMapping("/run")
	public Result<Object> run(@RequestBody SysJob job) throws SchedulerException {
		jobService.run(job);
		return SUCCESS;
	}

	/**
	 * 删除定时任务
	 */
	@PreAuthorize("@ss.hasPermi('monitor:job:remove')")
	@Log(title = "定时任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/remove/{jobIds}")
	public Result<Object> remove(@PathVariable Long[] jobIds) throws SchedulerException, TaskException {
		jobService.deleteJobByIds(jobIds);
		return SUCCESS;
	}

}
